package com.atldy.commerce.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.atldy.commerce.account.AddressInfo;
import com.atldy.commerce.account.AddressItemVo;
import com.atldy.commerce.commence.Id;
import com.atldy.commerce.commence.TableId;
import com.atldy.commerce.filter.AccessContent;
import com.atldy.commerce.mapper.EcommerceAddressMapper;
import com.atldy.commerce.pojo.EcommerceAddress;
import com.atldy.commerce.service.EcommerceAddressService;
import com.atldy.commerce.vo.LoginUserinfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 26321
 */
@Service
@Slf4j
public class EcommerceAddressServiceImpl extends ServiceImpl<EcommerceAddressMapper, EcommerceAddress>
        implements EcommerceAddressService {

    @Autowired
    private EcommerceAddressMapper ecommerceAddressMapper;


    /**
     * @author: 唐多令
     * @date: 2021/8/13 13:10
     * @param: [addressItemVo 用户地址信息Vo]
     * @description: 创建用户地址信息, 返回用户地址id
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Id createAddressInfo(AddressItemVo addressItemVo) {
        if (addressItemVo == null) {
            log.error("传递参数为NULL");
        }
        Long userId = getUserId();
        EcommerceAddress address = new EcommerceAddress();
        address = address.toEcommerceAddress(userId, addressItemVo);
        log.info("获取用户地址信息开始创建:[{}]", JSONUtil.formatJsonStr(JSONUtil.toJsonStr(address)));
        int insert = ecommerceAddressMapper.insert(address);
        if (insert != 1) {
            log.error(">>>>>>>>>>创建用户地址信息失败");
        }
        log.info("获取用户地址信息创建结束:[{}]", JSONUtil.formatJsonStr(JSONUtil.toJsonStr(address)));
        return new Id(address.getId());
    }

    private Long getUserId() {
        LoginUserinfo loginUserinfo = AccessContent.getLoginUserinfo();
        log.info("从本地缓存中取出用户信息：[{}]",JSONUtil.formatJsonStr(JSONUtil.toJsonStr(loginUserinfo)));
        return Convert.toLong(loginUserinfo.getId());
    }

    /**
     * @author: 唐多令
     * @date: 2021/8/13 13:10
     * @param: []
     * @description: 获取当前用户的地址信息
     */
    @Override
    public AddressInfo getCurrentUserAddressInfo() {
        Long userId = getUserId();
        AddressInfo addressInfo = new AddressInfo();
        List<AddressItemVo> addressItemVoList = new ArrayList<>();
        //构建查询参数
        QueryWrapper<EcommerceAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        ecommerceAddressMapper.selectList(wrapper)
                .stream().filter(ecommerceAddress -> ecommerceAddress != null)
                .forEach(ecommerceAddress -> {
                    AddressItemVo addressItemVo = ecommerceAddress.toAddressItemVo();
                    addressItemVoList.add(addressItemVo);
                });
        addressInfo.setUserId(userId);
        addressInfo.setAddressItemVos(addressItemVoList);
        return addressInfo;
    }

    /**
     * @return
     * @author: 唐多令
     * @date: 2021/8/13 13:23
     * @param: [id 地址信息主键id]
     * @description: 根据地址信息主键id获取用户地址信息
     */
    @Override
    public AddressItemVo getAddressInfoById(Id id) {
        if (id == null) {
            log.error("主键id为空");
            return null;
        }
        EcommerceAddress ecommerceAddress = ecommerceAddressMapper.selectById(id.getId());
        if (ecommerceAddress == null) {
            log.error("地址信息不存在");
            return null;
        }
        AddressItemVo addressItemVo = ecommerceAddress.toAddressItemVo();
        return addressItemVo;
    }

    /**
     * @author: 唐多令
     * @date: 2021/8/13 13:24
     * @param: [tableId 地址信息主键集合]
     * @description: 根据用户地址主键集合获取用户地址信息
     */
    @Override
    public AddressInfo getAddressInfoByIds(TableId tableId) {
        AddressInfo addressInfo = new AddressInfo();
        Long userId = getUserId();
        List<AddressItemVo> addressItemVos = new ArrayList<>();
        List<Id> ids = tableId.getIds();
        if (CollectionUtil.isEmpty(ids)) {
            log.error("主键id为空");
            return addressInfo;
        }
        List<Long> list = ids.stream().filter(id -> id != null).map(Id::getId).collect(Collectors.toList());
        List<EcommerceAddress> ecommerceAddressList = ecommerceAddressMapper.selectBatchIds(list);
        if (ecommerceAddressList == null) {
            log.error("地址信息不存在");
            return addressInfo;
        }
        ecommerceAddressList.stream().forEach(ecommerceAddress -> addressItemVos.add(ecommerceAddress.toAddressItemVo()));
        addressInfo.setAddressItemVos(addressItemVos);
        addressInfo.setUserId(userId);
        return addressInfo;
    }
}




